<template>
    <a-layout-content style="padding: 0 50px">
        <a-layout style="padding: 24px 0; background: #fff">
            <a-layout-content :style="{ padding: '0 24px', minHeight: '280px' }">
                <p>
                    <a-button type="primary" @click="add">添加</a-button>
                </p>
                <a-table :dataSource="specification"
                         :columns="columns"
                         :pagination="pagination"
                         :loading="loading"
                         @change="handleTableChange"
                >
                    <template v-slot:action="{text,record}">
                        <a-button type="primary" @click="edit(record.id)">编辑</a-button>
                        <a-popconfirm
                                title="删除后无法恢复，确认删除吗?"
                                ok-text="是"
                                cancel-text="否"
                                @confirm="del(record.id)"
                        >
                            <a-button type="danger">删除</a-button>
                        </a-popconfirm>
                    </template>
                </a-table>
            </a-layout-content>
        </a-layout>
        <a-modal v-model:visible="modelVisible"
                 title="品牌编辑表单"
                 @ok="handModelOk"
                 cancel-text="取消"
                 ok-text="确定"
        >
            <a-form :model="specEntity" :label-col="{span:6}" :wrapper-col="wrapperCol">
                <a-form-item label="规格名称">
                    <a-input v-model:value="specEntity.specification.specName" />
                </a-form-item>

                <a-form-item label="规格选项">
                    <a-button type="primary" :size="small" @click="addTableRow">新增规格选项</a-button>
                    <table id="tb01" width="340px" >
                        <thead>
                        <tr>
                            <td>规格选项</td>
                            <td>排序</td>
                            <td>操作</td>
                        </tr>
                        </thead>
                        <tbody>
                        <tr v-for="pojo in specEntity.specificationOptionList" v-bind:key="pojo.id">
                            <td><a-input v-model:value="pojo.optionName"/></td>
                            <td><a-input v-model:value="pojo.orders"/></td>
                            <td><a-button :size="small" danger block @click="deleTableRow($index)">删除</a-button></td>
                        </tr>
                        </tbody>
                    </table>
                </a-form-item>
            </a-form>
        </a-modal>
    </a-layout-content>
</template>

<script lang="ts">
    import { defineComponent, ref,onMounted } from 'vue';
    import axios from "axios";
    import {message} from "ant-design-vue";

    export default defineComponent ({
        name: "admin-specification",
        components:{
        },
        setup(){
            const columns= [
                {
                    title: '规格id',
                    dataIndex: 'id',
                    width:'10%'
                },
                {
                    title: '规格名称',
                    dataIndex: 'specName',
                    width:'65%'
                },
                {
                    title: '操作',
                    key:'action',
                    slots:{
                        customRender:'action'
                    }
                }
            ]
            const pagination = ref({
                current:1,
                pageSize:5,
                total:0
            });
            const loading = ref<boolean>(false);
            const modelVisible = ref<boolean>(false);
            const specification = ref();
            const specEntity = ref<any>({
                specification:{},
                specificationOptionList:[]
            })

            const add=()=>{
                modelVisible.value=true;
                specEntity.value.specification={};
                specEntity.value.specificationOptionList=[];
            }
            const edit=(id:number)=>{
                axios.get('http://localhost:8899/mall-manager/specification/findSpecById/'+id).then(reponse=>{
                    if (reponse.data.code==200){
                        specEntity.value = JSON.parse(JSON.stringify(reponse.data.data)); //原数据复制一份
                        modelVisible.value=true;
                    }else {
                        message.error(reponse.data.message)
                    }
                })

            }
            const del=(id:number)=>{
                axios.delete('http://localhost:8899/mall-manager/specification/deleteSpecById/'+id).then(reponse=>{
                    if (reponse.data.code==200){
                        //加载最新数据
                        handleQuery({
                            page:1,
                            size:pagination.value.pageSize
                        });
                    }else {
                        message.error(reponse.data.message)
                    }
                })
            }
            const handModelOk=()=>{
                axios.post('http://localhost:8899/mall-manager/specification/saveOrUpdateSpec',specEntity.value).then(reponse=>{
                    if (reponse.data.code==200){
                        modelVisible.value=false;
                        //加载最新数据
                        handleQuery({
                            page:1,
                            size:pagination.value.pageSize
                        });
                    }else {
                        message.error(reponse.data.message)
                    }

                })
            }
            const handleQuery =(params:any)=>{
                loading.value=true;
                axios.get("http://localhost:8899/mall-manager/specification/findSpecificationByPage",{
                    params:{
                        page:params.page,
                        size:params.size
                    }
                }).then((response)=>{
                    loading.value=false;
                    specification.value=response.data.list;
                    //重置分页按钮
                    pagination.value.current=params.page;
                    pagination.value.total=response.data.total;
                })
            }
            const handleTableChange = (pagination:any)=>{
                handleQuery({
                    page:pagination.cuechange,
                    size:pagination.size
                })
            }
            //添加新行
            const addTableRow = ()=>{
                specEntity.value.specificationOptionList.push({});
            };

            //删除行
            const deleTableRow = (index:number) =>{
                specEntity.value.specificationOptionList.splice(index,1);
            };
            onMounted(()=>{
                handleQuery({
                    page:1,
                    size:pagination.value.pageSize
                });
            })
            return{
                pagination,
                columns,
                loading,
                modelVisible,
                specification,
                specEntity,
                handleQuery,
                handleTableChange,
                addTableRow,
                deleTableRow,
                handModelOk,
                add,
                edit,
                del
            }
        }
    })
</script>

<style scoped>

</style>